package com.example.myapplication;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import com.example.myapplication.R;

import java.util.Calendar;

public class CalendarView extends View {
    private static final int DAYS_IN_WEEK = 7;
    private static final int MAX_WEEKS_IN_MONTH = 6;

    private Paint dayPaint;
    private Paint headerPaint;
    private Paint todayPaint;
    private Paint selectedPaint;

    private Calendar currentDate;
    private Calendar today;
    private Calendar selectedDate;

    private int cellSize;
    private int headerHeight;

    private String[] dayNames = {"日", "一", "二", "三", "四", "五", "六"};

    public CalendarView(Context context) {
        super(context);
        init();
    }

    public CalendarView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    private void init() {
        // 初始化日期对象
        currentDate = Calendar.getInstance();
        today = (Calendar) currentDate.clone();
        selectedDate = (Calendar) currentDate.clone();

        // 初始化绘制工具
        dayPaint = new Paint();
        dayPaint.setAntiAlias(true);
        dayPaint.setTextSize(40);
        dayPaint.setColor(Color.BLACK);
        dayPaint.setTextAlign(Paint.Align.CENTER);

        headerPaint = new Paint();
        headerPaint.setAntiAlias(true);
        headerPaint.setTextSize(45);
        headerPaint.setColor(Color.BLUE);
        headerPaint.setTextAlign(Paint.Align.CENTER);
        headerPaint.setStyle(Paint.Style.FILL_AND_STROKE);

        todayPaint = new Paint();
        todayPaint.setAntiAlias(true);
        todayPaint.setStyle(Paint.Style.FILL);
        todayPaint.setColor(Color.parseColor("#E3F2FD"));

        selectedPaint = new Paint();
        selectedPaint.setAntiAlias(true);
        selectedPaint.setStyle(Paint.Style.FILL);
        selectedPaint.setColor(Color.parseColor("#BBDEFB"));
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        // 计算单元格大小
        cellSize = getWidth() / DAYS_IN_WEEK;
        headerHeight = cellSize;

        // 绘制日历头部（星期）
        drawDayNames(canvas);

        // 绘制日历天数
        drawDays(canvas);
    }

    private void drawDayNames(Canvas canvas) {
        for (int i = 0; i < DAYS_IN_WEEK; i++) {
            float x = i * cellSize + cellSize / 2;
            float y = headerHeight / 2 + headerPaint.getTextSize() / 3;
            canvas.drawText(dayNames[i], x, y, headerPaint);
        }
    }

    private void drawDays(Canvas canvas) {
        Calendar tempDate = (Calendar) currentDate.clone();
        tempDate.set(Calendar.DAY_OF_MONTH, 1);

        int firstDayOfWeek = tempDate.get(Calendar.DAY_OF_WEEK) - 1; // 转换为0-based
        int daysInMonth = tempDate.getActualMaximum(Calendar.DAY_OF_MONTH);

        // 计算偏移量
        int offset = firstDayOfWeek;

        for (int day = 1; day <= daysInMonth; day++) {
            int row = (offset + day - 1) / DAYS_IN_WEEK;
            int col = (offset + day - 1) % DAYS_IN_WEEK;

            float x = col * cellSize + cellSize / 2;
            float y = (row + 1) * cellSize + cellSize / 2 + headerPaint.getTextSize() / 3;

            // 检查是否是今天
            boolean isToday = isSameDay(tempDate, day, today);
            boolean isSelected = isSameDay(tempDate, day, selectedDate);

            // 绘制背景
            if (isToday) {
                canvas.drawRect(col * cellSize, (row + 1) * cellSize,
                        (col + 1) * cellSize, (row + 2) * cellSize, todayPaint);
            } else if (isSelected) {
                canvas.drawRect(col * cellSize, (row + 1) * cellSize,
                        (col + 1) * cellSize, (row + 2) * cellSize, selectedPaint);
            }

            // 绘制日期数字
            canvas.drawText(String.valueOf(day), x, y, dayPaint);
        }
    }

    private boolean isSameDay(Calendar month, int day, Calendar date) {
        return month.get(Calendar.YEAR) == date.get(Calendar.YEAR) &&
                month.get(Calendar.MONTH) == date.get(Calendar.MONTH) &&
                day == date.get(Calendar.DAY_OF_MONTH);
    }

    public void setSelectedDate(int year, int month, int day) {
        selectedDate.set(year, month, day);
        invalidate();
    }

    public void goToPreviousMonth() {
        currentDate.add(Calendar.MONTH, -1);
        invalidate();
    }

    public void goToNextMonth() {
        currentDate.add(Calendar.MONTH, 1);
        invalidate();
    }
    public Calendar getCurrentDate() {
        return (Calendar) currentDate.clone();
    }
}
